#include<vector>
#include<iostream>
using std::vector;


class Solution {
public:
    int trap(vector<int>& height) {
        //vector<int> leftMaxH(height.size(), 0), rightMaxH(height.size(), 0);
        int n = height.size();
        int leftMaxH[n], rightMaxH[n];
        
        for (int i = 0; i < height.size(); i++) {
            // 左边
            leftMaxH[i] = ((i > 0 && height[i] >= leftMaxH[i-1]) || i == 0) ? height[i] : leftMaxH[i-1];
            rightMaxH[n-i-1] = ((i > 0 && height[n-i-1] >= rightMaxH[n-i]) || i == 0) ? height[n-i-1] : rightMaxH[n-i];
        }
        int area = 0;
        for (int i = 0; i < n; i++) {
            auto h = (leftMaxH[i] < rightMaxH[i]) ? leftMaxH[i] : rightMaxH[i];
            area += (h > height[i]) ? (h - height[i]) : 0;
        }
        return area;
    }
};


int main() {
    vector<int> input = {};
    int n;
    while (std::cin >> n) {
        input.push_back(n);
    }
    std::cout << "input: \n";
    for (auto k: input) std::cout << k << " ";
    std::cout << "\n";
    

    auto res = Solution().trap(input);
    std::cout << "result is " << res << "\n";

}